<?php/*  PHP version 5  Copyright (c) 2002-2019ECISP.CN、EarcLink.COM  警告：这不是一个免费的软件，请在许可范围内使用，请尊重知识产权，侵权必究，举报有奖  作者：黄祥云 E-mail:6326420@qq.com  QQ:6326420 TEL:18665655030 / 13797552220*/


use application\ESPCMS_Core;
use application\ESPCMS_PageButton;
use modul_application\ESPCMS_Dialog;
use modul_application\ESPCMS_ArticleTypeDB;
use inc_application\link\ArticleLink;
use inc_application\link\OrderLink;
use inc_application\link\EnquiryLink;

class Search {

	public static function in_List() {
		global $espcms_web_templates, $espcms_link_db;
		$db_table = ESPCMS_DB_PREFIX . 'document AS a';
		$db_table_model_att = ESPCMS_DB_PREFIX . 'document_model_att';
		$db_table_document_attr = ESPCMS_DB_PREFIX . 'document_attr';
		$keyword = check_str($_REQUEST['keyword']);
		if (!$keyword) {
			ESPCMS_Dialog::Message_Page('db_err');
		}
		$keyname = $_REQUEST['keyname'] && in_array($_REQUEST['keyname'], array('title', 'summary', 'tsn')) ? $_REQUEST['keyname'] : 'title';
		$lng = espcms_lancode();

		$pageid = $_GET['pageid'] && espcms_ismatches($_GET['pageid']) ? $_GET['pageid'] : 1;

		$per_page_num = 10;

		$lanpack = $espcms_web_templates->geto('lngpack');
		$lanpack['sitename'] = $keyword . '-' . $lanpack['sitename'];
		$espcms_web_templates->into('path', 'article');

		$order_app_config = ESPCMS_Core::get_app_config('order', false);
		$enquiry_app_config = ESPCMS_Core::get_app_config('enquiry', false);

		$db_where = " AND a.isclass=1 AND a.isbase=0 AND a.lng='$lng' AND a.$keyname LIKE '%$keyword%'";
		if (espcms_ismatches($_REQUEST['mid']) && $_REQUEST['mid']) {
			$db_where .= ' AND a.mid=' . $_REQUEST['mid'];
		}
		if (espcms_ismatches($_REQUEST['sid']) && $_REQUEST['sid']) {
			$db_where .= ' AND a.sid=' . $_REQUEST['sid'];
		}
		if (espcms_ismatches($_REQUEST['tid']) && $_REQUEST['tid']) {
			$tid_array = ESPCMS_ArticleTypeDB::get_article_type_listDB($_REQUEST['tid'], $lng);
			$tid_list = implode(',', array_keys($tid_array));
			$db_where .= is_array($tid_array) && count($tid_array) > 1 ? " AND a.tid IN ($tid_list)" : " AND a.tid=$tid_list";
		}
		if ($_GET['attr_array']) {
			$_REQUEST['attr_array'] = unserialize(stripslashes($_GET['attr_array']));
		}
		$attr_array = $_REQUEST['attr_array'] && is_array($_REQUEST['attr_array']) ? $_REQUEST['attr_array'] : array();

		if (is_array($attr_array) && count($attr_array) > 0) {

			foreach ($attr_array as $key => $value) {
				if (!espcms_isact($key, "/^[\w]+$/i")) {
					continue;
				}
				if ($value) {
					$db_att_where = " AND isclass=1 AND attrname='$key'";
					$countnum = espcms_db_num($db_table_model_att, $db_att_where);
					if ($countnum > 0) {
						if (is_array($value) && count($value) > 0) {
							$db_where_or = '';
							foreach ($value as $i => $where_val) {
								$where_val = check_str($where_val);
								$db_where_or .= $i > 0 ? " OR FIND_IN_SET('$where_val',b.$key)" : " FIND_IN_SET('$where_val',b.$key)";
							}
							$db_where .= " AND ($db_where_or)";
						} else {
							$value = check_str($value);
							$db_where .= " AND b.$key='$value'";
						}
					}
				}
			}
		}
		
		$left_db_table = $db_table . " LEFT JOIN " . $db_table_document_attr . " AS b ON a.did=b.did";

		$db_num = espcms_db_num($left_db_table, $db_where);

		$db_sql = "SELECT b.*,a.* FROM " . $db_table . " LEFT JOIN " . $db_table_document_attr . " AS b ON a.did=b.did WHERE 1=1 " . $db_where;

		$espcms_page_button = new ESPCMS_PageButton($db_sql, $db_num, $per_page_num, $pageid, 1, $lanpack['pagebotton'], 'inc_application\link\ArticleLink', 'search_link', $_REQUEST);

		$sql = $espcms_page_button->sql_creat('a.pid, a.did', 'DESC');
		$db_query = $espcms_link_db->db_query($sql);
		while ($fetch_row = $espcms_link_db->db_array_list($db_query)) {
			$fetch_row['ctitle'] = empty($fetch_row['color']) ? $fetch_row['title'] : "<font color='" . $fetch_row['color'] . "'>" . $fetch_row['title'] . "</font>";
			$type_read = ESPCMS_ArticleTypeDB::get_article_type_view($fetch_row['tid']);
			$fetch_row['ishtml'] = $type_read['pageclass'] == 2 ? 2 : $fetch_row['ishtml'];
			$fetch_row['typename'] = $type_read['typename'];
			$fetch_row['typelink'] = ArticleLink::type_link($type_read);
			$fetch_row['link'] = ArticleLink::doc_link($fetch_row);
			if ($order_app_config['isetup'] && $order_app_config['isopen']) {
				$fetch_row['buylink'] = OrderLink::get_order_link('buy', $fetch_row);
			}
			if ($enquiry_app_config['isetup'] && $enquiry_app_config['isopen']) {
				$fetch_row['enqlink'] = EnquiryLink::get_enquiry_link('into', $fetch_row);
			}
			$array[] = $fetch_row;
		}
		$espcms_web_templates->into('lngpack', $lanpack);
		$espcms_web_templates->into('array', $array);
		$espcms_web_templates->into('path', 'search');
		$espcms_web_templates->into('attr_array', $attr_array);
		$espcms_web_templates->into('keyword', $keyword);
		$espcms_web_templates->into('keyname', $_REQUEST['keyname']);
		$espcms_web_templates->into('tid', $_REQUEST['tid']);
		$espcms_web_templates->into('mid', $_REQUEST['mid']);
		$espcms_web_templates->into('page_read', $espcms_page_button->page_argument_out());
		$espcms_web_templates->into('pagebotton', $espcms_page_button->page_botton_creat());
		$espcms_web_templates->output('article/search', 'search', false, false);
	}

}
